import 'package:audioplayers/audioplayers.dart';
import 'package:vibration/vibration.dart';

class Player {
  AudioPlayer player = AudioPlayer();
  bool isPlay = false;

  Player() {
    player.setReleaseMode(ReleaseMode.loop);
  }

  start() {
    if (isPlay) return;
    isPlay = true;
    player.play(
      AssetSource('error.mp3'),
      mode: PlayerMode.lowLatency,
    );
  }

  stop() {
    isPlay = false;
    player.stop();
  }
}

class Shocker {
  bool isPlay = false;
  start() async {
    if (isPlay) return;
    final hasV = await Vibration.hasVibrator();

    if (hasV ?? false) {
      Vibration.vibrate(duration: 1000, repeat: 2000);
    }
  }

  stop() {
    isPlay = false;
    Vibration.cancel();
  }
}

class AlertUtils {
  static AlertUtils? _manger;
  AlertUtils._();
  static AlertUtils share() {
    _manger ??= AlertUtils._();
    return _manger!;
  }

  bool sleepSockerError = false;
  bool sleepMusiceEror = false;
  bool fatigueSockerError = false;
  bool fatigueMusicError = false;

  final player = Player();
  final shocker = Shocker();

  setSleepSocker(bool error) {
    sleepSockerError = error;
    ckeckshocker();
  }

  setFagtigueSocker(bool error) {
    fatigueSockerError = error;
    ckeckshocker();
  }

  setSleepMusic(bool error) {
    sleepMusiceEror = error;
    ckeckMusic();
  }

  setFagtigueMusic(bool error) {
    fatigueMusicError = error;
    ckeckMusic();
  }

  ckeckMusic() {
    if (sleepMusiceEror || fatigueMusicError) {
      player.start();
    } else {
      player.stop();
    }
  }

  ckeckshocker() {
    if (sleepSockerError || fatigueSockerError) {
      shocker.start();
    } else {
      shocker.stop();
    }
  }
}
